<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打开进程文件指针</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pclose.html">pclose</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.readfile.html">readfile</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.popen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">popen</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">popen</span> &mdash; <span class="dc-title">打开进程文件指针</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.popen-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>popen</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$command</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code></span>
   )</div>

  <p class="para rdfs-comment">
   打开一个指向进程的管道，该进程由派生给定的 command 命令执行而产生。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.popen-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">command</code></em></dt>

     <dd>

      <p class="para">
       命令。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">mode</code></em></dt>

     <dd>

      <p class="para">
       模式。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.popen-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个和 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>
   所返回的相同的文件指针，只不过它是单向的（只能用于读或写）并且必须用
   <span class="function"><a href="function.pclose.html" class="function">pclose()</a></span> 来关闭。此指针可以用于
   <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span>，<span class="function"><a href="function.fgetss.html" class="function">fgetss()</a></span> 和
   <span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span>。
   当模式为 &#039;r&#039;，返回的文件指针等于命里的 STDOUT，当模式为 &#039;w&#039;，返回的文件指针等于命令的 STDIN。
  </p>
  <p class="para">
   如果出错返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.popen-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2355">
    <p><strong>Example #1 <span class="function"><strong>popen()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">popen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/bin/ls"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
  如果未找到要执行的命令，会返回一个合法的资源。这看上去很怪，但有道理。它允许访问
    shell 返回的任何错误信息：
   <div class="example" id="example-2356">
    <p><strong>Example #2 <span class="function"><strong>popen()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;加入重定向以得到标准错误输出&nbsp;stderr。&nbsp;*/<br /></span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">popen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/executable&nbsp;2&gt;&amp;1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"'</span><span style="color: #0000BB">$handle</span><span style="color: #DD0000">';&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">gettype</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$read&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2096</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$read</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">pclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.popen-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    如果需要双向支持，使用 <span class="function"><a href="function.proc-open.html" class="function">proc_open()</a></span>。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"><a href="features.safe-mode.html" class="link">安全模式</a> 启用时，可仅可用
<a href="ini.sect.safe-mode.html#ini.safe-mode-exec-dir" class="link">safe_mode_exec_dir</a> 执行文件。实际上，现在不允许在到可执行的路径中存在 <em>..</em> 组件。</span></p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara"><a href="features.safe-mode.html" class="link">安全模式</a> 启用时，命令字符串会被
<span class="function"><a href="function.escapeshellcmd.html" class="function">escapeshellcmd()</a></span> 转换。因此，<em>echo y | echo x</em> 会变成
<em>echo y \| echo x</em>。</p></div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.popen-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.pclose.html" class="function" rel="rdfs-seeAlso">pclose()</a> - 关闭进程文件指针</span></li>
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="function.proc-open.html" class="function" rel="rdfs-seeAlso">proc_open()</a> - Execute a command and open file pointers for input/output</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.pclose.html">pclose</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.readfile.html">readfile</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
